package com.hafiery.sdk.bluetooth.util;

/**
 * @author peng.wang
 */
public class ByteUtil {
    public static String hexToString(byte[] value) {
        StringBuilder builder = new StringBuilder();
        for (byte item: value){
            String hex = Integer.toHexString(item & 0x000000ff);
            if (hex.length() == 1) {
                builder.append("0");
            }
            builder.append(hex);
            builder.append(" ");
        }
        return builder.toString();
    }

    public static byte[] intToBytesLittle(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }
    public static byte[] intToBytes(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    public static int byteFourToIntLittle(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
            | ((src[offset + 1] & 0xFF) << 8)
            | ((src[offset + 2] & 0xFF) << 16)
            | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }

    public static int byteTwoToIntLittle(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
            | ((src[offset + 1] & 0xFF) << 8));
        return value;
    }

    public static byte stringToHex(String hex) {
        if (hex.length() != 2) {
            throw new RuntimeException();
        }

        int value = charToByte(hex.charAt(0)) * 16 +  charToByte(hex.charAt(1));
        return (byte)(0x000000ff & value);
    }

    private static int charToByte(char c) {
        if (c >= '0' && c<='9') {
            return c - '0';
        } else if (c>='A' && c<='F') {
            return c- 'A' + 10;
        } else if (c>='a' && c<='f') {
            return c- 'a' + 10;
        } else {
            throw new RuntimeException();
        }
    }

    public static String bytesToHexString(byte[] bArr) {
        StringBuffer sb = new StringBuffer(bArr.length);
        String sTmp;

        for (int i = 0; i < bArr.length; i++) {
            sTmp = Integer.toHexString(0xFF & bArr[i]);
            if (sTmp.length() < 2)
                sb.append(0);
            sb.append(sTmp.toUpperCase());
        }

        return sb.toString();
    }

    public static int bytes2int(byte[] src){
        int firstByte = 0;
        int secondByte = 0;
        int thirdByte = 0;
        int fourthByte = 0;
        int index = 0;
        int anUnsignedInt = 0;

        firstByte = (0x000000FF & ((int) src[index]));
        secondByte = (0x000000FF & ((int) src[index+1]));
        thirdByte = (0x000000FF & ((int) src[index+2]));
        fourthByte = (0x000000FF & ((int) src[index+3]));

        anUnsignedInt  = (((int) (firstByte<<24|secondByte<<16|thirdByte<<8|fourthByte)) & 0x00000000FFFFFFFF);
        return anUnsignedInt  ;
    }
}
